var placemarks = new Array();
var addresses  = new Array();
var geocoder;
var course;

function setStart(address) {
  // Get lat, lon of address
  if(!geocoder) {
    geocoder = new GClientGeocoder();
  }
  geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        log('setStart: ' + address + ' not found');
      } else {
        // Create placemark
	createPlacemark("Start", point.x, point.y);
	createPlacemark("Car", point.x, point.y);
        addresses[addresses.length] = address;
      }
    }
  );  
}

function setFinish(address) {
  geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        log('setFinish: ' + address + ' not found');
      } else {
        // Create placemarkp
	createPlacemark("Finish!", point.x, point.y);
        addNextWaypoint(address);
      }
    }
  );  
}

function addNextWaypoint(address) {
  log ('addCourseLocation: ' + address);
  addresses[addresses.length] = address;
  var lineString = ge.createLineString('');

  if(!course) {
    course = ge.createPlacemark('');
  }

  course.setGeometry(lineString);
  lineString.setTessellate(true);

  // Get lat, lon of address - 1
  geocoder.getLatLng(
    addresses[addresses.length - 1],
    function(point) {
      if (!point) {
        log('addNextWaypoint: ' + address + ' not found');
      } else {
        log('Adding course point at [' + point.x + ',' + point.y + ']');
        // Push point onto line coordinates
        lineString.getCoordinates().
          pushLatLngAlt(point.x, point.y, 0);
      }
    }
  );

  // Get lat, lon of address 
  geocoder.getLatLng(
    addresses[addresses.length],
    function(point) {
      if (!point) {
        log('addNextWaypoint: ' + address + ' not found');
      } else {
        log('Adding course point at [' + point.x + ',' + point.y + ']');
        // Push point onto line coordinates
        lineString.getCoordinates().
          pushLatLngAlt(point.x, point.y, 0);
      }
    }
  );

}

function drawCourse() {
  log('drawCourse');
  ge.getFeatures().appendChild(course);
}

function createPlacemark(name, lat, lon) {
  var placemark = ge.createPlacemark('');
  placemark.setName(name);
  ge.getFeatures().appendChild(placemark);

  // Create style map for placemark
  var normal = ge.createIcon('');
  normal.setHref('http://maps.google.com/mapfiles/kml/paddle/red-circle.png');
  var iconNormal = ge.createStyle('');
  iconNormal.getIconStyle().setIcon(normal);
  var highlight = ge.createIcon('');
  highlight.setHref('http://maps.google.com/mapfiles/kml/paddle/red-circle.png');
  var iconHighlight = ge.createStyle('');
  iconHighlight.getIconStyle().setIcon(highlight);
  var styleMap = ge.createStyleMap('');
  styleMap.setNormalStyle(iconNormal);
  styleMap.setHighlightStyle(iconHighlight);
  placemark.setStyleSelector(styleMap);

  // Create point
  var point = ge.createPoint('');
  point.setLatitude(lat);
  point.setLongitude(lon);
  log('Created placemark at:<br/>&nbsp;&nbsp;'+ lat +','+ lon);
  placemark.setGeometry(point);
  placemarks[placemark.getName()] = placemark;
}

function createPlacemarkAddr(name, address) {
  var placemark = ge.createPlacemark('');
  placemark.setName(name);
  ge.getFeatures().appendChild(placemark);

  // Create style map for placemark
  var normal = ge.createIcon('');
  normal.setHref('http://maps.google.com/mapfiles/kml/paddle/red-circle.png');
  var iconNormal = ge.createStyle('');
  iconNormal.getIconStyle().setIcon(normal);
  var highlight = ge.createIcon('');
  highlight.setHref('http://maps.google.com/mapfiles/kml/paddle/red-circle.png');
  var iconHighlight = ge.createStyle('');
  iconHighlight.getIconStyle().setIcon(highlight);
  var styleMap = ge.createStyleMap('');
  styleMap.setNormalStyle(iconNormal);
  styleMap.setHighlightStyle(iconHighlight);
  placemark.setStyleSelector(styleMap);

  // Get lat, lon of address
  if(!geocoder) {
    geocoder = new GClientGeocoder();
  }
  geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        log(address + ' not found');
      } else {
        log('Created placemark at:<br/>&nbsp;&nbsp;'+ point.x +','+ point.y);
        // Create placemarkp
        placemark.setGeometry(point);
        placemarks[placemark.getName()] = placemark;
      }
    }
  );  

}


function createLine(lat1, lon1, lat2, lon2) {
  var lineStringPlacemark = ge.createPlacemark('');
  var lineString = ge.createLineString('');
  lineStringPlacemark.setGeometry(lineString);
  lineString.setTessellate(true);
  lineString.getCoordinates().
    pushLatLngAlt(lat1, lon1, 0);
  lineString.getCoordinates().
    pushLatLngAlt(lat2, lon2, 0);
  ge.getFeatures().appendChild(lineStringPlacemark);
}

function getPlacemark(name) {
  return placemarks[name];
}

function clearPlacemarks() {
  for (var p in placemarks) {
    ge.getFeatures().removeChild(placemarks[p]);
    log('removed: '+ p);
    delete placemarks[p];
  }
}
